package com.mohan.tool;

import java.io.UnsupportedEncodingException;

import org.apache.commons.codec.binary.Base64;

public class Base64CC {
	 private static final String ENCODING = "UTF-8";
	    
	    /**
	     * 一般Base64加密
	     */
	    public static String encode(String data) throws UnsupportedEncodingException{
	    	if (data == null) {
				return null;
			} else {
				byte[] encodedByte = Base64.encodeBase64(data.getBytes(ENCODING));
		        return new String(encodedByte, ENCODING);
			}
	    }
	    
	    /**
	     * Base64解密
	     */
	    public static String decode(String data) throws UnsupportedEncodingException{
	        byte[] decodedByte = Base64.decodeBase64(data.getBytes(ENCODING));
	        return new String(decodedByte, ENCODING);
	    }
	    
	    /**
	     * 安全Base64加密
	     */
	    public static String encodeSafe(String data) throws UnsupportedEncodingException{
	        /*
	         * 注意：这里采用的encodeBase64(byte[] bytes, boolean arg1)
	         * arg1为true时，加密后的字符串每行为76个字符，不论每行够不够76个字符，都要在行尾添加“\r\n”
	         */
	        byte[] encodedByte = Base64.encodeBase64(data.getBytes(ENCODING),true);
	        return new String(encodedByte, ENCODING);
	    }
}
